package com.example.score_reception.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.score_reception.entity.*;
import com.example.score_reception.mapper.TeacherMapper;
import com.example.score_reception.service.RoleService;
import com.example.score_reception.service.TeacherRoleService;
import com.example.score_reception.service.TeacherService;
import com.example.score_reception.util.ContextHolderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService, UserDetailsService {
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private RoleService roleService;
    @Autowired
    private TeacherRoleService teacherRoleService;
    @Override
    public UserDetails loadUserByUsername(String s) throws AuthenticationException {
        HttpServletRequest request = ContextHolderUtils.getRequest();
        String password = request.getParameter("teacherPassword");
        BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();
        Map<String,Object> map = new HashMap<>();
        map.put("teacher_account",s);
        List<Teacher> teacherList = teacherMapper.selectByMap(map);
        /**
         * 因为不想用security本身的msg消息，就提前做了一下想要的msg
         */
        if(s == null || password == null) {
            throw new BadCredentialsException("用户名或密码不能为空");
        }
        if(teacherList.size() == 0 || teacherList.get(0) == null) {
            throw new BadCredentialsException("帐号不存在，请重新输入");
        }
        if(!bcryptPasswordEncoder.matches(password, teacherList.get(0).getPassword())) {
            throw new BadCredentialsException("密码错误");
        }
        if(teacherList.get(0).getTeacherStatus() == 0) {
            throw new BadCredentialsException("账号已被封禁");
        }
        List<TeacherRole> teacherRoleList = teacherRoleService.list(new LambdaQueryWrapper<TeacherRole>().eq(TeacherRole::getTeacherId,teacherList.get(0).getTeacherId()));
        List<Long> roleIds = new ArrayList<>();
        for (TeacherRole userRole : teacherRoleList) {
            roleIds.add(userRole.getRoleId());
        }
        List<Role> roles = roleService.list(new LambdaQueryWrapper<Role>().in(Role::getRoleId,roleIds));
        teacherList.get(0).setRoles(roles);
        return teacherList.get(0);
    }
}
